/* Copyright 2017, 2018, 2019, 2020 Gabriel Czernikier
 *
 * This file is part of Côtehaus.
 * Côtehaus is free software: you can redistribute it and/or modify
 * it under the terms of one of the following:
 * 1. The same dual license as that of Côtehaus itself, of which individual licenses are
 * the MIT License and the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 2. The MIT License.
 * 3. The GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Côtehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MIT License and the GNU General Public License for more details.
 * You should have received a copy of the MIT License and the GNU General Public License
 * along with Côtehaus.  If not, see <https://opensource.org/licenses/MIT> and <http://www.gnu.org/licenses/>.
 */
#ifndef __CH_COMPOSER_H
#define __CH_COMPOSER_H

#include <stdbool.h>
#include <stdlib.h>
#include "lgwseq.h"
#include "types.h"
// Exported variables
extern int up_bounds_ary[];
extern int alignment_ary[];
extern int majors_size_ary[];
extern unsigned char *buffer[];
extern size_t buf_len[];
extern size_t buf_offset[];
// Constants to track and frame computed bounds
#define NH_UNUSED_IDX 0
#define NH_HEADER_IDX 1
#define NH_STR_ID_IDX 2
#define NH_TYPE_ID_IDX 3
#define NH_PROTO_ID_IDX 4
#define NH_FIELD_ID_IDX 5
#define NH_METHOD_ID_IDX 6
#define NH_CLASS_DEF_IDX 7
#define NH_TYPE_LIST_IDX 8
#define NH_MAP_LIST_IDX 9
#define NH_STR_DT_IDX 10
#define NH_CLS_DT_IDX 11
#define NH_UP_BOUNDS_SZ 12
#define NH_DATA_LBOUND (NH_TYPE_LIST_IDX-1)
#define NH_DATA_UBOUND (NH_UP_BOUNDS_SZ-1)
// Constants that set known-on-beforehand values for the sake of computing bounds (aside the values themselves could be in turn an item data to other items).
#define NH_HEADER_UP_BOUND 0x70
void bounds_move(int idx, unsigned int amount);
void align_bounds();

#define BUFFER_WRITE(BUFFER,BUFLEN,DATA,OFFSET,LEN) while((OFFSET)+(LEN)>(BUFLEN)) \
	(BUFLEN) = (BUFLEN)==0?512:(BUFLEN)*2; \
	if(((BUFFER) = realloc((BUFFER), (BUFLEN)))==NULL) { \
		fprintf(stderr, "%s: %s.", __func__, strerror(ENOMEM)); \
		exit(EXIT_FAILURE); \
	} \
	memcpy((BUFFER)+(OFFSET), (DATA), (LEN)); \
	(OFFSET) += (LEN);

#define NO_INDEX 0xffffffff
#endif
